package com.ultrapower.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ultrapower.commons.base.BaseController;
import com.ultrapower.model.LlBlackListDm;
import com.ultrapower.service.IBlackListService;
import com.ultrapower.task.UploadBlackList;

/**
 * @description：测试Controller
 * @author：zhixuan.wang
 * @date：2015/10/1 14:51
 */
@Controller
@RequestMapping("/test")
public class TestController extends BaseController {
	
	@Autowired
    private IBlackListService blackListService;

    /**
     * 图标测试
     * 
     * @RequiresRoles shiro 权限注解
     * 
     * @return
     */
    @RequiresRoles("test")
    @GetMapping("/dataGrid")
    public String dataGrid() {
        return "admin/test";
    }

    /**
     * 模板下载
     * @return
     */
    @GetMapping("/downTmp")
    public ResponseEntity<Resource> downTmp() {
        File file = new File("D:/template/demo.xlsx");
        return download(file);
    }
    
    
    @GetMapping("/downBlackListData")
    public ResponseEntity<Resource> downBlackListData(HttpServletRequest request) {
    	String path = request.getSession().getServletContext().getRealPath("downloadfileDir"); //获取服务器指定文件存取路径
    	File downloadFile = new File(path);
    	if(!downloadFile.exists()){
    		downloadFile.mkdir();
    	}
    	List<LlBlackListDm> bls = blackListService.selectBlackList();
    	System.out.println("导出黑名单的数量：" + bls.size());
//        File file = new UploadEnterprise().createDownloadExcel(downloadFile+"\\data.xlsx", enterprises);
//        return download(file);
    	File file = new UploadBlackList().createDownloadExcel(downloadFile+"\\data.xlsx", bls);
    	return download(file);
    }
    
    /**
     * 黑名单模板下载
     * @return
     */
    @GetMapping("/downBlackListTmp")
    public ResponseEntity<Resource> downBlackListTmp(HttpServletRequest request) {
    	String path = request.getSession().getServletContext().getRealPath("blackListTmp"); //获取服务器指定文件存取路径
        File file = new File(path+ File.separator +"BlackList.xlsx");
        if(!file.getParentFile().exists()){
        	file.getParentFile().mkdirs();
        }
        return download(file);
    }
}
